package com.itlong.cloud.app.service.impl;

import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.app.robot.AppRobotAccountInfoGetVO;
import com.itlong.cloud.POJO.VO.app.robot.AppRobotProjectInfoVO;
import com.itlong.cloud.app.dao.IAppRobotLoginDao;
import com.itlong.cloud.app.service.IAppRobotLoginService;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *     机器人APP登录服务层接口实现类
 * </desc>
 *
 * @createDate 2018/10/15
 */
@Service
public class AppRobotLoginServiceImpl implements IAppRobotLoginService {

    @Autowired
    private IAppRobotLoginDao iAppRobotLoginDao;

    /**
     * <desc>
     *      获取机器人登录账号信息
     * </desc>
     *
     * @param account 账号
     * @return 账号信息
     * @author wangzhi
     * @createDate 2018/10/15
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public AppRobotAccountInfoGetVO getRobotUserByAccount(String account) throws Exception {
        AppRobotAccountInfoGetVO appRobotAccountInfoGetVO = iAppRobotLoginDao.getRobotUserByAccount(account);
        if (appRobotAccountInfoGetVO == null || StringUtils.isBlank(appRobotAccountInfoGetVO.getAppId())){
            return null;
        }
        //获取项目列表
        List<AppRobotProjectInfoVO> projectInfoVOS = iAppRobotLoginDao.getProjectIdList(appRobotAccountInfoGetVO.getAppId());
        List<AppRobotProjectInfoVO> projectInfoVOList = new ArrayList<>();
        if (projectInfoVOS != null){
            for (AppRobotProjectInfoVO appRobotProjectInfoVO : projectInfoVOS){
                if (appRobotProjectInfoVO.getStatus() == 1 && appRobotProjectInfoVO.getProjectStatus() == 1){
                    projectInfoVOList.add(appRobotProjectInfoVO);
                }
            }
        }
        if (!projectInfoVOList.isEmpty()){
            appRobotAccountInfoGetVO.setProjectId(projectInfoVOList.get(0).getProjectId());
            appRobotAccountInfoGetVO.setCurrentProjectName(projectInfoVOList.get(0).getProjectName());
            appRobotAccountInfoGetVO.setRobotId(projectInfoVOList.get(0).getRobotId());
        }
        return appRobotAccountInfoGetVO;
    }

    /**
     * <desc>
     *      获取项目id列表
     * </desc>
     *
     * @param userId 开发者账号用户id
     * @return
     * @Author Zhu.yj
     * @createDate  2020/8/3
     */
    @Override
    public List<AppRobotProjectInfoVO> getProjectIdList(String userId) throws Exception {
        return iAppRobotLoginDao.getProjectIdList(userId);
    }

    /**
     * <desc>
     *      切换项目
     * </desc>
     *
     * @param projectId 项目id
     * @return
     * @Author Zhu.yj
     * @createDate  2020/8/4
     */
    @Override
    public AppRobotProjectInfoVO changeProject(String projectId, String userId) throws Exception {
        List<AppRobotProjectInfoVO> robotProjectInfoVOS = iAppRobotLoginDao.getProjectIdList(userId);
        AppRobotProjectInfoVO appRobotProjectInfoVO = new AppRobotProjectInfoVO();
        for (AppRobotProjectInfoVO projectInfoVO : robotProjectInfoVOS){
            if (projectId.equals(projectInfoVO.getProjectId())){
                appRobotProjectInfoVO = projectInfoVO;
            }
        }
        return appRobotProjectInfoVO;
    }

}
